/**
 * Moondance UML modeling tool
 * 
 * This program is free software; you can redistribute it and
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 3
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package ro.pub.cs.se.moondance.editor;

import org.eclipse.jface.action.IAction;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.ui.IEditorActionDelegate;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
import org.eclipse.ui.IWorkbenchWindowPulldownDelegate;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.actions.ActionDelegate;

import ro.pub.cs.se.moondance.MoondanceData;
import ro.pub.cs.se.moondance.code.CodeGeneratorFactory;
import ro.pub.cs.se.moondance.code.ICodeGenerator;
import ro.pub.cs.se.moondance.model.UMLClassDiagram;

public abstract class EditorCodeGenActionDelegate extends ActionDelegate
		implements IEditorActionDelegate {

	// Code generation exit status
	public static final String SUCCESS = "Success";
	public static final String ERROR = "Error";

	public static final String ERROR_INVALID_DIAGRAM = "Code can be generated only for class diagrams!";
	public static final String ERROR_INVALID_GENERATION = "Code generation worker thread failed!";
	public static final String SUCCESS_PREFIX = " project successfully generated from current class diagram!";

	public void run(IAction action) {
	}

	protected void runWrapper(String language) {
		MessageBox box = new MessageBox(PlatformUI.getWorkbench()
				.getActiveWorkbenchWindow().getShell());

		// Open a box with a message depending on the operation exit code
		if (MoondanceData.getCurrentDiagram() instanceof UMLClassDiagram) {
			try {
				ICodeGenerator codeGen = CodeGeneratorFactory
						.newCodeGenerator(language);
				codeGen.start();
				codeGen.join();
				box.setText(SUCCESS);
				box.setMessage(language + SUCCESS_PREFIX);
			} catch (Exception e) {
				e.printStackTrace();
				box.setText(ERROR);
				box.setMessage(ERROR_INVALID_GENERATION);
			}
		} else {
			System.out.println(MoondanceData.getCurrentDiagram());
			box.setText(ERROR);
			box.setMessage(ERROR_INVALID_DIAGRAM);
		}

		box.open();
	}

	/**
	 * @see IWorkbenchWindowPulldownDelegate#getMenu(Control)
	 */
	public Menu getMenu(Control parent) {
		return null;
	}

	/**
	 * @see IWorkbenchWindowActionDelegate#dispose()
	 */
	public void dispose() {
		// Nothing to do...
	}

	@Override
	public void setActiveEditor(IAction action, IEditorPart targetEditor) {
		// Nothing to do...
	}
}
